<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
    <link rel="stylesheet" href="../../css/global.css" media="all"/>
</head>
<body>
<div class="form-filter-wrap" style="margin-top: 10px">
    <form class="layui-form form-inline page-form" onsubmit="return false" id="form">
        <input type="hidden" id="id" name="id">
        <div class="form-group">
            <div class="form-filter">
                <div class="form-inner">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" placeholder="账号" type="text" name="username" id="username"
                                lay-verify="required">
                    </div>
                </div>
                <div class="form-inner">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" placeholder="用户名" type="text" name="nickname" id="nickname"
                               lay-verify="required">
                    </div>
                </div>
                <div class="form-inner">
                    <label class="layui-form-label">U聊账号</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" placeholder="U聊账号" type="text" maxlength="20"
                               name="ul" id="ul" lay-verify="required">
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="form-filter">
                <div class="form-inner">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" placeholder="邮箱" type="text" maxlength="50"
                               name="email" id="email" lay-verify="email">
                    </div>
                </div>
                <div class="form-inner">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <select name="status" id="status"></select>
                    </div>
                </div>
                <div class="form-inner">
                    <label class="layui-form-label">所属单位</label>
                    <div class="layui-input-inline">
                        <select multiple lay-omit name="branch" id="branch" lay-filter="branch" lay-search></select>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="form-filter">
                <label class="layui-form-label">角色</label>
                <div class="checkbox" id="roles" style="line-height: 34px;">
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="form-filter">
                <div class="form-inner">
                    <label class="layui-form-label">审核人</label>
                    <div class="layui-input-inline">
                        <select  lay-omit name="userId" id="userId" lay-filter="required">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-actions">
            <div class="row" align="center">
                <div class="col-md-12">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                </div>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="../../js/libs/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/my/roles/role.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/dict.js"></script>
<script type="text/javascript">
    layui.use(['layer', 'laydate', 'form'], function () {
        var form = layui.form;

        form.on('submit(formDemo)', function (data) {
            data.field.roleIds = getCheckedRoleIds();
            if (!$("#branch").val()) {
                layer.msg("请选择单位！");
                return false;
            }
            data.field.branch = $("#branch").val().toString();
            $.ajax({
                type: 'put',
                url: '/users',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data.field),
                success: function (data) {
                    layer.msg("修改成功！", {time: 1000, icon: 1}, function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
                        parent.layer.close(index); //再执行关闭
                    });
                }
            })
            return false;
        });
    });

    initRoles();

    //刷新表单
    function renderForm() {
        layui.use('form', function () {
            var form = layui.form; //高版本建议把括号去掉，有的低版本，需要加()
            form.render()
        });
    }

    layui.use(['form', 'upload', 'layer'], function () {
        var form = layui.form;
        $.when(
            /*公司*/
            $.ajax({
                url: '/dropDown/getBranch',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    // 使用循环遍历，给下拉列表赋值
                    $('#branch').append(new Option("请选择", ""));
                    $.each(data, function (index, value) {
                        $('#branch').append(new Option(value.value, value.key));// 下拉菜单里添加元素
                    });
                    form.render("select");//重新渲染 固定写法
                }
            })
            /*状态*/
            , $.ajax({
                url: '/dropDown/getStatus',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    // 使用循环遍历，给下拉列表赋值
                    $.each(data, function (index, value) {
                        $('#status').append(new Option(value.value, value.key));// 下拉菜单里添加元素
                    });
                    form.render("select");//重新渲染 固定写法
                }
            })
            , $.ajax({
                url: '/dropDown/getUser',
                dataType: 'json',
                type: 'post',
                success: function (data) {

                    // 使用循环遍历，给下拉列表赋值
                    $.each(data, function (index, value) {
                        $('#userId').append(new Option(value.value, value.key));// 下拉菜单里添加元素
                    });
                    form.render("select");//重新渲染 固定写法
                }
            })
        ).done(function () {
            initData()
        });
    });

    function initData() {
        var id = getUrlParam("id");
        if (id != "") {
            $.ajax({
                type: 'get',
                url: '/users/' + id,
                async: false,
                success: function (data) {
                    $("#id").val(data.id);
                    $("#username").val(data.username);
                    $("#nickname").val(data.nickname);
                    $("#ul").val(data.ul);
                    $("#email").val(data.email);
                    $("#status").val(data.status);
                    if (data.branchs) {
                        $('#branch').val(data.branchs);
                    }
                    $("#userId").val(data.userId);
                }
            });

            $.ajax({
                type: 'get',
                url: '/roles?userId=' + id,
                success: function (data) {
                    var length = data.length;
                    for (var i = 0; i < length; i++) {
                        $("input[type='radio']").each(function () {
                            var v = $(this).val();
                            if (v == data[i]['id']) {
                                $(this).attr("checked", true);
                                renderForm();
                            }
                        });
                    }
                }
            });
        }
    }

</script>
</body>
</html>